#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#include <unistd.h>

#include "GuiFrontPanel.h"
#include "Heartbeat.h"
#include "vale.h"

static int g_argc;
static char **g_argv;
void ExitMain(int sigNo);

static pthread_t g_threadHeartBeat;
static pthread_t g_threadGuiFrontPanel;
static pthread_t g_threadVale;

int main(int argc, char *argv[])
{
    g_argc = argc;
    g_argv = argv;
    signal(SIGINT, ExitMain);
    printf(">>> [%s] start running\n", argv[0]);

    pthread_create(&g_threadHeartBeat, NULL, (void*)HeartBeatMain, NULL);
    pthread_create(&g_threadGuiFrontPanel, NULL, (void*)GuiFrontPanelMain, NULL);
    pthread_create(&g_threadVale, NULL, (void*)ValeCentralMain, NULL);
    pthread_join(g_threadVale, NULL);
    pthread_join(g_threadGuiFrontPanel, NULL);
    pthread_join(g_threadHeartBeat, NULL);

    return 0;
}

void ExitMain(int sigNo)
{
    if (sigNo == SIGINT) {
        printf("\n");
        GuiFrontPanelExit();
        HeartBeatExit();
        printf(">>> [%s] stoped running\n", g_argv[0]);
        exit(0);
    }
}

